#pragma once

#include "../NavData.h"
#include "../../Static/Mathf.h"
#include "../../Log/Log.hpp"

namespace ns_Time
{
    using namespace ns_Data;
    using namespace ns_Log;

    class TimeSys : public NavData
    {
    public:
        using SecondCount = double;
        // 到基准时间的秒数
        virtual SecondCount SecondFromRefTime() const = 0;
        // 经过一段秒数后的时间
        virtual TimeSys *TimeAfterSecond(SecondCount seconds) const = 0;
        // 获取基准时间
        virtual TimeSys *GetRefTime() const = 0;
    public:
        bool Encode(std::string* outJson) const override
        {
            Json::Value outValue;

            outValue["Type"] = "TimeSys";
            outValue["SecondsFromRefTime"] = SecondFromRefTime();

            Json::FastWriter writer;
            *outJson = writer.write(outValue);

            return true;
        }

        bool Decode(const std::string& inJson) override
        {
            Json::Value inValue;
			Json::Reader reader;
			bool parsingSuccessful = reader.parse(inJson, inValue);
			if (!(parsingSuccessful && inValue["Type"].asString() == "TimeSys"))
				return false;

            SecondCount seconds = inValue["SecondsFromRefTime"].asDouble();

            auto tmp = GetRefTime()->TimeAfterSecond(seconds);
            *this = *tmp;

            return true;
        }
    protected:
        static const int MinuteSecond = 60;
        static const int HourSecond = 3600;
        static const int DaySecond = 86400;   // 一天的时间
        static const int WeekSecond = 604800; // 一周的时间
    };
}